package com.jiangdong.eureka.consumer.proxy;

import com.jiangdong.eureka.consumer.entitys.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class StudentProxy implements InvocationHandler {

    private Object delegate;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public Object bind(Object delegate) {
        this.delegate = delegate;
        return Proxy.newProxyInstance(delegate.getClass().getClassLoader(), delegate.getClass().getInterfaces(), this);
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result;
        try {
            logger.info("Before Proxy");
            result = method.invoke(delegate, args);
            logger.info("After Proxy");
        } catch (Exception e) {
            throw e;
        }
        return result;
    }

    public static void main(String[] args) {
        StudentProxy personProxy = new StudentProxy();
        IStudent iStudent = (IStudent) personProxy.bind(new Student());
        System.out.println(iStudent.doSomething());
    }
}
